package com.ws.shop.action.front;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ws.shop.bean.Page;
import com.ws.shop.util.RequestUtil;

@Controller
@RequestMapping(value = "/payment/*")
public class PaymentAction  extends BaseFrontAction {

	@RequestMapping(value="paymentindex.htm",method=RequestMethod.GET)//请求地址
	public String paymentindex(HttpServletRequest request,Model model){
		int pageno = RequestUtil.getIntParameter(request, Page.PAGE_NO, 1);
		Page page = new Page(pageno,10);
		int state = 1;
		//sunpicService.sunpics(page, state);
		model.addAttribute("page", page);
		return "messageindex";
	}
	
	/* public function alipay_notify_url()
	    {
	    	$data = Init_GP(array('out_trade_no','total_fee')); //out_trade_no定单号 total_fee总价
	    	import("@.ORG.Alipay");
	    	$alipay = new Alipay();
	    	$info = $alipay->_notify_url();
	   		if ($info){
	    		//成功执行需要操作的方法
	    		$this->succ($data['out_trade_no'],$data['total_fee']);
	    	}else{
	    		//失败方法
	    	}
	    }
	    
	 	public function alipay_return_url()
	    {
			$data = Init_GP(array('out_trade_no','total_fee'));
	    	import("@.ORG.Alipay");
	    	$alipay = new Alipay();
	    	$info = $alipay->_return_url();
	    	if ($info === true){
	    		//成功执行需要操作的方法
	    		$this->succ($data['out_trade_no'],$data['total_fee']);
	    		$json = '支付成功';
	    	}elseif ($info === false){
	    		//失败
	    		$json = '支付失败';
	    	}else{
	    		$json = $info; //返回状态码
	    	}
	    	$this->assign('json',$json);
	    	$this->display();
	    }

		public function yeepay_return_url()
	    {
			$data = Init_GP(array('r6_Order','r3_Amt'));
	    	import("@.ORG.Yeepay");
	    	$yeepay = new Yeepay();
	    	$info = $yeepay->_return_url();
	    	if ($info === true){
	    		//成功执行需要操作的方法
	    		$this->succ($data['r6_Order'],$data['r3_Amt']);
	    		$json = '支付成功';
	    	}elseif ($info === false){
	    		//失败
	    		$json = '支付失败';
	    	}else{
	    		$json = $info; //返回状态码
	    	}
	    	$this->assign('json',$json);
	    	$this->display();
	    }
	    
		public function chinabank_return_url()
	    {
			$data = Init_GP(array('v_oid','v_amount'));
	    	import("@.ORG.Chinabank");
	    	$chinabank = new Chinabank();
	    	$info = $chinabank->_return_url();
	    	if ($info === true){
	    		//成功执行需要操作的方法
	    		$this->succ($data['v_oid'],$data['v_amount']);
	    		$json = '支付成功';
	    	}elseif ($info === false){
	    		//失败
	    		$json = '支付失败';
	    	}else{
	    		$json = $info; //返回状态码
	    	}
	    	$this->assign('json',$json);
	    	$this->display();
	    }
	    
		public function tenpay_return_url()
	    {
			$data = Init_GP(array('sp_billno','total_fee'));
	    	import("@.ORG.Tenpay");
	    	$tenpay = new Tenpay();
	    	$info = $tenpay->_return_url();
	    	if ($info === true){
	    		//成功执行需要操作的方法
	    		$this->succ($data['sp_billno'],$data['total_fee'] / 100);
	    		$json = '支付成功';
	    	}elseif ($info === false){
	    		//失败
	    		$json = '支付失败';
	    	}else{
	    		$json = $info; //返回状态码
	    	}
	    	$this->assign('json',$json);
	    	$this->display();
	    }
	    
	    public function succ($sn,$amount,$type){
	    	$pre = substr($sn,0,2);
	    	if ($pre == 'RE'){
	    		//查找哪条充值订单
	    		$paymentorder = D('Paymentorder');
	    		$map = array(
	    			'sn'=>array('eq',$sn),
	    			'state'=>array('eq',0)
	    		);
	    		$data = $paymentorder->where($map)->find();
	    		if (!empty($data) && $data['amount'] == $amount){
	    			//修改订单状态
	    			$paymentordermap['id']=array('eq',$data['id']);
	    			$paymentorderdata = array(
	    				'state'=>1,
	    				'successtime'=>time()
	    			);
	    			$paymentorder->where($paymentordermap)->save($paymentorderdata);
	    			//修改用户信息
					$pid = $data['pid'];
	    			$user = D('User');
	    			$config = C('SYSCONFIG');
	    			if (empty($pid)){
		    			$beat = $data['amount'] * $config['rmb_beat'];
		    			$user->_setbeat($data['uid'],$beat);
		    			$remark = "用户{$data['payment']}充值{$beat}{$config['beat_name']}";
	    			}else {
	    				$package = D('Package');
			    		$packagedata = $package->find($pid);
	    				$beat = $packagedata['beat'];
	    				$pointb = $packagedata['pointb'];
	    				$user->setInc('beat',"id = {$data['uid']}",$beat);
	    				$user->setInc('pointb',"id = {$data['uid']}",$pointb);
	    				$remark = "用户{$data['payment']}充值{$beat}{$config['beat_name']},赠送{$pointb}{$config['pointb_name']}";
	    			}
	    			//添加交易日志
	    			$transaction = D('Transaction');
	    			$transactiondata = array(
						'uid'=>$data['uid'],
						'name'=>$data['name'],
						'payment'=>'充值',
	    				'money'=>$data['amount'],
						'remark'=>$remark
					);
					$transaction->insert($transactiondata);
	    		}
	    	}elseif ($pre == 'PA'){
	    		$order = D('Order');
	    		$map = array(
	    			'ordersn'=>array('eq',$sn),
	    			'state'=>array('eq',0)
	    		);
	    		$data = $order->where($map)->find();

	    		if (!empty($data) && $data['orderprice'] == $amount){
	    			//修改订单状态
	    			$ordermap['id']=array('eq',$data['id']);
	    			$orderdata = array(
	    				'state'=>1
	    			);
	    			$order->where($ordermap)->save($orderdata);
	    			//添加交易日志
	    			$transaction = D('Transaction');
	    			$transactiondata = array(
							'uid'=>$data['uid'],
							'name'=>$data['name'],
							'payment'=>'支付订单',
	    					'money'=>$data['orderprice'],
							'remark'=>"用户支付{$data['goodsname']}定单号{$data['ordersn']}"
						);
					$transaction->insert($transactiondata);
	    		}
	    	}
	    }*/
}
